// 需求列表
var demandColumns = [
    {
        id: 'createTime',
        title: '提交时间',
        type: 'date',
        width: 100,
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms',
        columnClass: 'text-center'
    },
    {
        id: 'createUser',
        title: '提交人',
        type: 'string',
        width: 75,
        columnClass: 'text-center'
    },
    {
        id: 'fromName',
        title: '来源项目',
        type: 'string',
        width: 150,
        columnClass: 'text-center'
    },
    {
        id: 'delName',
        title: '接收项目',
        type: 'string',
        width: 150,
        columnClass: 'text-center'
    },
    {
        id: 'dmLevel',
        title: '优先级',
        type: 'string',
        width: 60,
        columnClass: 'text-center',
        resolution: function (value) {
            var content = '';
            if (value == 1) {
                content += '低';
            }
            if (value == 2) {
                content += '中';
            }
            if (value == 3) {
                content += '高';
            }
            return content;
        }
    },
    {
        id: 'version',
        title: '基本版本号',
        type: 'string',
        width: 90,
        columnClass: 'text-center'
    },
    {
        id: 'dmDesc',
        title: '需求描述',
        type: 'string',
        columnClass: 'text-left'
    },
    {
        id: 'operation',
        title: '操作',
        type: 'string',
        width: 60,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-primary" onclick="ckdemand(\'' + record.processId + '\')"><i class="icon-eye-open"></i> 查看</button>';
            return content;
        }
    }
];
function findDeman(CertusJsonAjax) {
    CertusJsonAjax.dataInit("findDemanList",{},demandColumns,'demandContainer','demandToolBarContainer')
}

function ckdemand(id) {
    location.href="#/ckdemand/"+id;
}

function toAddDemand() {
    location.href="#/addDemand"
};
function addDemand(CertusFormAjax,$scope) {

    //所属公司
    CertusFormAjax.ajax("companyListByViewCode",{},function (response) {
        $scope.comp = JSON.parse(response.data);
    });

    this.changetp = function () {
        CertusFormAjax.ajax("findProByType",{"proType":$("#proType").val()},function (response) {
            $scope.prorlist = response.data;
        });
    }

    this.getProMg = function(){
        CertusFormAjax.ajax("findMg",{"proId":$("#rproId").val()},function (response) {
            $scope.userName = response.data.userName;
            $scope.userId = response.data.userId;
        });
    }

    // $('.typeahead').typeahead({
    //     source: function(query, process) {
    //         return CertusFormAjax.ajax("companyList",{},function (response) {
    //             var data = JSON.parse(response.data);
    //             var results = $.map(data, function (product) {
    //                 var arr={};
    //                 arr.id = product.companyId;
    //                 arr.company = product.company
    //                 return arr;
    //             });
    //             process(results);
    //         });
    //     },
    //     updater: function (obj) {
    //         var item = JSON.parse(obj);
    //         return item.id;
    //     }
    // });

    CertusFormAjax.ajax("findSeProList",{},function(response){
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].proName;
            arr.id = data[i].id;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#proName').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#proId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });

    });

    this.getDept = function () {
        CertusFormAjax.ajax("deptedList",{companyCode:$("#companys").val()},function (response) {
            $scope.dept = JSON.parse(response.data);
        });
    }

    var fileInstantiation = {
        classCateCode : 'NBCPYZ',//选定市场类 文档
        fileClassCode : 'LCFJ' //选定合同文件
    }
    var fileInstantiationDisable = {
        classCateCodeDis : true, //文件类目不可修改
        fileClassCodeDis : true //文件类型不可修改
    }
    //调用初始化方法
    initFileUpload($scope, fileInstantiation,fileInstantiationDisable , CertusFormAjax);

    var fileId = "";
    $(".toRegu").on('click',function () {
        fileUpload($scope,function (res){
            fileId = res.data;
        },null,CertusFormAjax);
    })
    $(".submit").on('click',function () {
        if (!certusValidateNotnull()){
            return;
        }
        if ($("#proType").val()==""){
            layer.msg("请选择接收项目类型！");
            return;
        }
        var arr =$("form").serializeArray();
        var data = {};
        for (var i=0;i<arr.length;i++){
            data[arr[i].name] = arr[i].value;
        };
        data.fileUrl = fileId;
        CertusFormAjax.ajax("addDeman",{"data":JSON.stringify(data)},function(response){
            layer.msg("提交成功");
            window.history.back();
        });
    })
}

function findDept() {
    getDept();
}
//文件上传统一字段
function showfile() {
    $(".hidefile").show();
    $("#isUpload").val(1);
}
hidefile();
function hidefile() {
    $(".hidefile").hide();
    $("#isUpload").val(0);
}

// 需求信息处理记录
// var editDemandColumns = [{
//     id: 'doTime',
//     title: '操作时间',
//     width: 160,
//     type: 'string',
//     columnClass: 'text-center',
// }, {
//     id: 'dohistory',
//     title: '操作记录',
//     type: 'string',
//     columnClass: 'text-left',
// }, ];
// var editDemandOption = {
//     lang: 'zh-cn',
//     ajaxLoad: false,
//     // loadURL:'url',
//     exportFileName: '需求信息处理记录',
//     datas: [{
//         doTime: '9999-99-99 24:00',
//         dohistory: '张三提交需求申请',
//     }, ],
//     columns: editDemandColumns,
//     gridContainer: 'editDemandContainer',
//     toolbarContainer: 'editDemandToolBarContainer',
//     tools: '',
//     pageSize: 10,
//     pageSizeLimit: [10, 15, 20]
// };
// var editDemandGrid = $.fn.dlshouwen.grid.init(editDemandOption);
// editDemandGrid.load();
